package com.etc.service;


import com.etc.dao.CommentsDao;
import com.etc.dao.MoviesDao;
import com.etc.dao.OrdersDao;
import com.etc.entity.Comments;
import com.etc.entity.Movies;
import com.etc.entity.Orders;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    OrdersDao ordersDao;
    @Autowired
    CommentsDao commentsDao;
    @Autowired
    MoviesDao moviesDao;

    @Override
    public PageInfo<Orders> queryAllByUser(Integer user_no, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> list = ordersDao.queryAllByUser(user_no);
        PageInfo<Orders> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Orders> queryAllByShowing(Integer showing_no, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> list = ordersDao.queryAllByShowing(showing_no);
        PageInfo<Orders> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Orders queryDetail(Integer user_no, Integer showing_no) {
        return ordersDao.queryDetail(user_no, showing_no);
    }

    @Override
    public Orders queryDetail(String order_no) {
        return ordersDao.queryByOrder(order_no);
    }

    @Transactional
    @Override
    public boolean addOrder(Orders orders) {
        int n = ordersDao.addOrder(orders);
        return n > 0;
    }

    @Transactional
    @Override
    public boolean delOrder(Integer order_no) {
        int n = ordersDao.delOrder(order_no);
        return n > 0;
    }

    @Override
    public PageInfo<Orders> queryByUser(Integer user_no, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> list=ordersDao.queryByUser(user_no);
        for (Orders orders:list){
            Comments comments=commentsDao.queryDtail(orders.getUser_no(), orders.getOrder_no());
            orders.setComments(comments);
        }
        PageInfo<Orders> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Orders> queryByMovie(Integer movie_no, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> list=ordersDao.queryByMovie(movie_no);
        PageInfo<Orders> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Orders queryByOrder(String order_no) {
        return ordersDao.queryByOrder(order_no);
    }

    @Override
    public boolean updateOrderStatus(Orders orders) {
        int i = ordersDao.updateOrderStatus(orders);
        return i>0;
    }

    @Override
    public List<Movies> queryAllCount() {
        List<Movies> list = moviesDao.selectMovieByName("");
        for(Movies m:list){
            Integer co = ordersDao.queryCount(m.getMovieNo());
            m.setShowing_count(co);
        }
        return list;
    }
}
